package com.aacar.utils;

import java.math.BigDecimal;


/**
 * Copyright (C), 2015年3月14日, 人人养宠
 * @author fei.tang
 */
public class MapUtil {
    
    private static final double PI = 3.14159265358979323; //圆周率
    private static final double R = 6371229;              //地球的半径
   
   
    public static double getDistance(double longt1, double lat1, double longt2, double lat2){
    	if(longt1==0.0||lat1==0.0||longt2==0.0||lat2==0.0){
    		return 0;
    	}
        double x,y,distance;
        x=(longt2-longt1)*PI*R*Math.cos( ((lat1+lat2)/2) *PI/180)/180;
        y=(lat2-lat1)*PI*R/180;
        distance=Math.hypot(x,y)/1000;
        return  new BigDecimal(distance).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     * 计算两点之间距离
     * @param _lon1 - start经度
     * @param _lat1 - start纬度
     * @param _lon2 - end经度
     * @param _lat2 - end纬度
     * @return km(四舍五入)
     */
    public static double getDistance2(double _lon1,double _lat1, double _lon2, double _lat2){
	     double lat1 = (Math.PI/180)*_lat1;
	     double lat2 = (Math.PI/180)*_lat2;
	     double lon1 = (Math.PI/180)*_lon1;
	     double lon2 = (Math.PI/180)*_lon2;
	     //地球半径
	     double R = 6378.1;
	     double d = Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R;
	     return new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
   
    public static double getLongt(double longt1, double lat1, double distance){
        double a = (180*distance)/(PI*R*Math.cos(lat1*PI/180));
        return a;
    }
   
   
    public static double getLat(double longt1, double lat1, double distance){
        double a = (180*distance)/(PI*R*Math.cos(lat1*PI/180));
        return a;
    }
    
    public static void main(String[] args) {
    	System.out.println(getDistance(121.433476, 31.231148,121.42274, 31.22464) + "km");
    	System.out.println(getDistance2(121.433476, 31.231148,121.42274, 31.22464) + "km");
	}
}
